package com.hp.attendance.dao;

import com.hp.attendance.entity.Department;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class DepartmentDao extends BaseDAO {
    //查询部门表
    public List<Department> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from department";
        List<Department> list = queryList(sql, new Object[0], new Department());
        return list;
    }

    //部门分页查询
    public List<Department> getDepartmentList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql="select * from department limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Department> Departments = queryList(sql, obj, new Department());
        return Departments;
    }


    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from department";
        int count = queryCount(sql, new Object[0]);
        return count;
    }

    public Department getDepartmentMAX() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "SELECT * FROM department WHERE department_number=(SELECT MAX(department_number)FROM department)";
        Department department = queryOne(sql, new Object[0], new Department());
        return department;
    }
    public void DepartmentAdd(Department department) throws SQLException, IOException, ClassNotFoundException {
          String sql="insert into department values(null,?,?,null,?,?,?)";
          Object[] obj ={department.getDepartment_number(),department.getName(),
                  department.getTelephone(),department.getAddress(),department.getNotes()};
          addOrUpdOrDel(sql,obj);
    }
    public void DepartmentDel(int id) throws SQLException, IOException, ClassNotFoundException {
        String sql="delete from department where id =?";
        Object[] obj = {id};
        addOrUpdOrDel(sql,obj);
    }
    public void DepartmentUpdate(Department department) throws SQLException, IOException, ClassNotFoundException {
        String sql="update department set department_number=?,name=?,telephone=?,address=?,notes=? where id =?";
        Object[] obj ={department.getDepartment_number(),department.getName(),
                department.getTelephone(),department.getAddress(),department.getNotes(),department.getId()};
        addOrUpdOrDel(sql,obj);
    }

    public Department departmentPreUpd(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql="select * from department where id =?";
        Object[] obj={id};
        Department department = queryOne(sql, obj, new Department());
        return department;
    }
}
